/**
 * @author xuzj
 */
/**
 * 仅用于展示的Form，支持多列并排显示
 */
CQCIS.form.DisForm = Ext.extend(Ext.FormPanel, {
    initState: Ext.emptyFn,
    saveState: Ext.emptyFn,
    labelWidth: 90,
    fieldWidth: 160,
    baseCls: 'x-panel',
    border: false,
    frame: true,
    layout: 'table',
    defaults: {
        bodyStyle: 'padding:0px;margin:0px;margin-right:5px;'
    },
    initComponent: function(){
        this.width = this.width ? this.width : 600;
        this.layoutConfig = this.layoutConfig ? this.layoutConfig : {};
        this.layoutConfig.columns = this.layoutConfig.columns ? this.layoutConfig.columns : this.columnLength ? this.columnLength : 1;
        this.initMetas();
        CQCIS.form.DisForm.superclass.initComponent.call(this);
        this.addEvents({
            /**
             * @event on load
             */
            "beforeload": true,
            "loadafter": true,
            /**
             * @event on save
             */
            "beforesave": true,
            "saveafter": true
        });
    },
    initMetas: function(){
        var records = [], items = [];
        for (var i = 0; i < this.metas.length; i++) {
            var m = this.metas[i];
            var o = Ext.applyIf(m.obj ||
            {}, {
                name: m.field,
                fieldLabel: m.desc
            });
            var r = {
                name: m.field,
                type: (m.type ? m.type : 'string')
            };
            
            if (!o.isFormField) {
                switch (m.xtype) {
                    case 'date':
                        o.xtype = 'datefield';
                        o.format = m.format ? m.format : "Y-m-d";
                        o.readOnly = true;
                        o.hideTrigger = true;
                        r.type = 'date';
                        break;
                    case 'time':
                        o.xtype = 'timefield';
                        o.readOnly = true;
                        o.format = m.format ? m.format : "Y-m-d H:i:s";
                        o.hideTrigger = true;
                        r.type = 'date';
                        break;
                    case 'area':
                        o.xtype = 'textarea';
                        break;
                    default:
                        if (m.xtype) {
                            o.xtype = m.xtype;
                        } else {
                            o.xtype = 'textfield';
                        }
                        break;
                }
            }
            
            if (m.vtype) {
                o.vtype = m.vtype;
            }
            o.width = m.obj ? (m.obj.width ? m.obj.width : this.fieldWidth) : this.fieldWidth;
            
            if (m.mapping) {
                r.mapping = m.mapping;
                o.mapping = m.mapping;
            }
            
            records[records.length] = r;
            if (!m.values) {
                o.msgTarget = 'side';
                o.readOnly = true;
                items.push({
                    layout: 'form',
                    colspan: m.obj && m.obj.colspan ? m.obj.colspan : null,
                    rowspan: m.obj && m.obj.rowspan ? m.obj.rowspan : null,
                    items: o
                });
            }
        }
        
        this.recordTypes = records;
        this.items = items;
    },
    getDs: function(){
        if (this.dwr && !this.ds) {
            this.ds = new CQCIS.data.DWRStore({
                dwr: {
                    call: this.dwr.load,
                    save: this.dwr.save ? this.dwr.save : this.dwr.update,
                    update: this.dwr.update ? this.dwr.update : this.dwr.save
                },
                fields: this.recordTypes
            });
            
            this.ds.on('load', function(scope, recodes){
                var recode = recodes[0];
                var data = recode ? recode.data : {};
                this.form.setValues(data);
                this.afterRemote();
                this.fireEvent('loadafter', data, this, scope);
            }, this);
            
            this.ds.on('save', function(scope, o){
                this.afterRemote();
                this.fireEvent('saveafter', o, this, scope);
            }, this);
        }
        
        return this.ds;
    },
    
    // private
    beforeRemote: function(waitMsg){
        if (waitMsg) {
            if (this.waitMsgTarget === true) {
                this.el.mask(waitMsg, 'x-mask-loading');
            } else if (this.waitMsgTarget) {
                this.waitMsgTarget = Ext.get(this.waitMsgTarget);
                this.waitMsgTarget.mask(waitMsg, 'x-mask-loading');
            } else {
                Ext.MessageBox.wait(waitMsg, '请稍候...');
            }
            this.doing = true;
        }
    },
    
    // private
    afterRemote: function(action, success){
        if (this.doing) {
            if (this.waitMsgTarget === true) {
                this.el.unmask();
            } else if (this.waitMsgTarget) {
                this.waitMsgTarget.unmask();
            } else {
                Ext.MessageBox.updateProgress(1);
                Ext.MessageBox.hide();
            }
            this.doing = false;
        }
    },
    
    // private
    doRemote: function(action){
        var valid = this.form.isValid();
        if (!valid) {
            CQCIS.msg('字段校验没通过,请查看窗口字段提示信息!');
        } else {
            this.beforeRemote('正在保存数据...');
            if (this.fireEvent("beforesave", this) !== false) {
                var arg = [this.getValues()];
                if (this.extendParams.length > 0) {
                    arg = arg.concat(this.extendParams);
                }
                this.getDs()[action]({
                    arg: arg
                });
            }
        }
    },
    
    /**
     * @param params
     *
     * <pre>
     * null
     * 代表新增
     * object/array  修改
     * </pre>
     */
    load: function(params){
        if (this.doing == true) {
            CQCIS.msg('正在远程访问,请稍侯!');
        } else {
            if (params) {
                this.beforeRemote('正在读取数据...');
                params = params instanceof Array ? params : [params];
                if (this.fireEvent("beforeload", this, params) !== false) {
                    this.getDs().load({
                        arg: params
                    });
                }
            } else {
                this.form.isValid(); // 增加校验提示
            }
        }
    }
});
